<?php

namespace app\admin\controller;

use app\common\BaseHelper as Helper;

use app\common\Common;
use app\admin\model\Member as MemberMdl;

use think\Request;

class Setting extends Base
{
    public function _initialize()
    {
        parent::_initialize();
        $this->model = 'operator';
        $defaultWhere = $this->getDefaultWhere();
        $this->defaultWhere = $defaultWhere;
        $authMdl = model('role');
        $authRole = $authMdl->where('status',1)->select();
        $this->assign('authRole',$authRole);
    }

    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        // Reset filter
        if ($request->param('reset')) {
            return redirect(fullUrl($request->path()));
        }
        if($request->isAjax()){
            $model = model($this->model);

            // 每页起始条数
            $start = ($param['page'] - 1) * $param['limit'];
            // 每页显示条数
            $length = $param['limit'] + 1;
            // 排序条件
            $order = '';

            $where = $this->getFilterWhere($request);
            if($this->defaultWhere){
                $where = array_merge($this->defaultWhere, $where);
            }
            if($this->defaultOrder){
                $order = $this->defaultOrder;
            }

            $fields = 'a.id,name,last_login_date,a.type,login_name,s.role_name';
            $data = $model->alias('a')
                ->where($where)
                ->join('role s','a.role_id = s.id','left')
                ->field($fields)
                ->limit($start, $length)
                ->order($order)
                ->select();

            $count = $model->where($where)->count();
            foreach($data as $k => $v)
            {
                $v['sort'] = $k + 1;
                if ($v['type'] === 0){
                    $v['role_name'] = '超级管理员';
                    $v['name'] = '超级管理员';
                }
            }
            $result = [
                'code' => '0',
                'msg' => '请求成功',
                'data' => $data,
                'count' => $count,
            ];

            return json($result);
        }

        return $this->fetch('list');

    }

    /**
     * Add
     */
    public function add(Request $request)
    {
            $role = db('role')->where('status',1)->select();
            $this->assign('authRole',$role);
            return parent::add($request);
    }

    /**
     * Add Post
     */
    public function addPost(Request $request, $redirect = '')
    {
        $param = $request->param();
        $model = model($this->model);
        if ($request->isAjax()){

            if (empty($param['login_name'])){
                return $this->error('账号为空');
            }
            if (empty($param['password'])){
                return $this->error('密码为空');
            }
            if (empty($param['role_id'])){
                return $this->error('角色为空');
            }
            $param['id'] = Helper::getUUID();
            $param['password'] = Helper::passwordEncrypt($param['password']);
            $param['create_date'] = time();
            $param['create_oper'] =$this->admin->id;
            $result = $model->save($param);
            if (!$result){
                return $this->error('添加失败');
            }
            $logInfo = $this->admin->name . '添加了一条' . lang($this->model) . '数据。';
            // Query执行后的操作
            $model->_after_update($param);

            // 写入日志
            Common::adminLog($request, $logInfo);
            return $this->success('添加成功！', 'admin/setting/index');
        }
    }

    /**
     * Edit
     */
    public function edit(Request $request){
        return parent::edit($request);
    }

    /**
     * Edit Post
     */
    public function editPost(Request $request, $redirect = ''){

        $param = $request->param();
        $model = model($this->model);
        if ($request->isAjax()){

            if (empty($param['login_name'])){
                return $this->error('账号为空');
            }
            if (empty($param['role_id'])){
                return $this->error('角色为空');
            }
            $param['create_date'] = time();
            $param['create_oper'] =$this->admin->id;
            if(empty(trim($param['password']))){
                unset($param['password']);
            }else{
                $param['password'] = Helper::passwordEncrypt($param['password']);
            }
            $result = $model->update($param,['id',$param['id']],true);
            if (!$result){
                return $this->error('修改失败');
            }
            $logInfo = $this->admin->name . '修改了一条' . lang($this->model) . '数据。';
            // Query执行后的操作
            $model->_after_update($param);

            // 写入日志
            Common::adminLog($request, $logInfo);
            return $this->success('修改成功！', 'admin/setting/index');
        }
    }

    //权限列表
    public function role(Request $request)
    {
        return view();
    }
}